package com.sinvo.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinvo.oa.entity.model.process.ProcessTemplate;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.mapper.ProcessTemplateMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.service.ProcessService;
import com.sinvo.oa.service.ProcessTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description ProcessTypeService实现类
 */
@Service
public class ProcessTemplateServiceImpl extends ServiceImpl<ProcessTemplateMapper, ProcessTemplate> implements ProcessTemplateService {

    @Autowired
    private ProcessTemplateMapper processTemplateMapper;

    @Autowired
    private ProcessService processService;

    @Override
    public Page<ProcessTemplate> selectPageProcessTemplate(Page<ProcessTemplate> page) {
        //直接查询分页数据
        Page<ProcessTemplate> processTemplatePage = this.page(page);
        //id查name
        List<ProcessTemplate> templateList = processTemplateMapper.queryTemplateForPageRecords();
        //手动封装records
        processTemplatePage.setRecords(templateList);
        //返回
        return processTemplatePage;
    }

    @Override
    public void saveTemplate(ProcessTemplate processTemplate) {
        //name唯一
        LambdaQueryWrapper<ProcessTemplate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessTemplate::getName,processTemplate.getName());
        int count = this.count(queryWrapper);
        if (count > 0){
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"模板名称重复");
        }
        boolean success = this.save(processTemplate);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "新增失败，请重试");
        }
    }

    @Override
    public void updateTemplate(ProcessTemplate processTemplate) {
        //name唯一
        LambdaQueryWrapper<ProcessTemplate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessTemplate::getName,processTemplate.getName());
        ProcessTemplate one = this.getOne(queryWrapper);
        if (one != null && one.getId() != processTemplate.getId()){
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"模板名称重复");
        }
        boolean success = this.updateById(processTemplate);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "更新失败，请重试");
        }
    }

    @Transactional
    @Override
    public void publish(Long id) {
        //获取对象 status设置为1
        ProcessTemplate processTemplate = this.getById(id);
        processTemplate.setStatus(1);
        //更新
        this.updateById(processTemplate);
        //获取压缩包所在路径
        String deploy = processTemplate.getProcessDefinitionPath();
        //部署流程
        if (!StringUtils.isEmpty(deploy)){
            processService.deployByZip(deploy);
        }
    }
}
